//
//  WebViewController.m
//  TCProject
//
//  Created by admin on 16/1/25.
//  Copyright © 2016年 wise-wing. All rights reserved.
//

#import "WebViewController.h"

@interface WebViewController ()<UIWebViewDelegate>

@property(nonatomic, weak) UIWebView *webView;

@end

@implementation WebViewController

- (void)loadView{
    UIWebView *webView = [[UIWebView alloc] init];
    webView.delegate = self;
    self.view = webView;
    _webView = webView;

}

- (void)viewDidLoad {
    [super viewDidLoad];
    if (_path) {
        [self.webView loadRequest:[NSURLRequest requestWithURL:_path]];
    } else {
        [self getData];
    }
}


- (void)getData {
    weakSelf(self);
    BMRequest *request = [BMRequest requestWithPath:@"getSinglePageByType.htm"];
    NSMutableString *typeStr = [NSMutableString stringWithString:kRoleType ? @"站点端" : @"车主端"];
    NSString *destStr;
    switch (_type) {
        case 1:
            destStr = [typeStr stringByAppendingString:@"使用指南"];
            break;
        case 2:
            destStr = [typeStr stringByAppendingString:@"关于平台"];
            break;
        case 3:
            destStr = [typeStr stringByAppendingString:@"注册协议"];
            break;
    }
    self.title = destStr;
    request.params = @{@"typeName" : destStr};
    [self showHudWithhint:kDefaultMessage];
    [[BMHttpTool sharedInstance] postWith:request finish:^(BMResponse *response, NSError *error) {
        [self hideHud];
        if (response.status == kResultOK) {
            NSString *content = response.rawResult[@"singlePage"][@"content"];
            if (![BMUtils isEmptyString:content]) {
                [weakSelf.webView loadHTMLString:content baseURL:nil];
            }
            self.title = response.rawResult[@"singlePage"][@"typeName"];
        } else {
            [self showHint:response.msg];
        }
    }];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    if ([BMUtils isEmptyString:self.title]) {
        NSString *title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
        self.title = title;
    }
}

@end
